<template>
  <el-dialog width="66%" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <!-- <el-form :inline="true" :model="queryParam" class="demo-form-inline"   >
      <el-form-item v-if="type === 'box'" label="盒号/盒名">
        <el-input
          v-model="queryParam.boxNoOrName"
          placeholder="请输入盒号或盒名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item v-if="type === 'archive'" label="档号/题名">
        <el-input
          v-model="queryParam.archiveNoOrName"
          placeholder="请输入档号或题名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form> -->
    <upToPosition ref="upToPosition" :data="tableData"></upToPosition>

    <el-table :data="tableData" border style="width: 100%" max-height="30vh" ref="table"
      @selection-change="selectionChange">
      <!-- <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"
        :selectable="checkSelectSet"
      >
      </el-table-column> -->
      <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
        :width="item.width">
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">确 认</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { EventBus } from '@/utils/EventBus'
import { archivesMove, boxMove } from '@/api/rfid/moveBill'

import upToPosition from '@/components/upToPosition.vue'

export default {
  name: 'moveDialog',
  components: {
    upToPosition,
  },
  data() {
    return {
      queryParam: {},
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      selectionData: [],
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      formDisabled: false,
      formRules: {
        positionId: [{ required: true, message: '请输入库位', trigger: 'blur' }],
      },
      tableData: [],
      tableProps: [],
      selectionChangeData: [],
    }
  },
  created() { },
  mounted() {
    // EventBus.$on('submitPostion', (param) => {
    //   // 使用参数
    //   this.queryParam.positionId = param.positionId
    //   // this.positionName = param
    // })
  },
  methods: {
    add(record, type) {
      this.selectionData = []
      this.type = type
      this.tableData = record
      this.$nextTick(() => {
        this.$refs.upToPosition.clearForm()
      })
      // this.selectionChangeData = record
      if (type === 'box') {
        this.dialogTitle = '选择档案盒'
        this.tableProps = [
          { title: '盒号', prop: 'boxNo', width: '', checked: true },
          { title: '盒名', prop: 'boxName', width: '', checked: true },
          { title: 'RFID', prop: 'rfid', width: '', checked: true },
          { title: '原位置', prop: 'positionId_dictText', width: '', checked: true },
        ]
        // this.boxLoadData()
      } else if (type === 'archive') {
        this.dialogTitle = '选择档案'
        this.tableProps = [
          { title: '档号', prop: 'archiveNo', width: '', checked: true },
          { title: '题名', prop: 'archiveName', width: '', checked: true },
          { title: 'RFID', prop: 'rfid', width: '', checked: true },
          { title: '原位置', prop: 'positionId_dictText', width: '', checked: true },
        ]
        // this.archiveLoadData()
      }
      this.dialogVisible = true
    },
    // onSubmit() {
    //   if (this.type === 'box') {
    //     this.boxLoadData()
    //   } else if (this.type === 'archive') {
    //     this.archiveLoadData()
    //   }
    // },

    // archiveLoadData() {
    //   var param = Object.assign(this.queryParam, this.searchModel)
    //   archivesMovePage(param)
    //     .then((res) => {
    //       if (res.data.code === 200) {
    //         this.tableData = res.data.data.records
    //       } else {
    //         this.$message.error(res.data.message)
    //       }
    //     })
    //     .catch((err) => {
    //       this.$message.error(err)
    //     })
    // },
    selectionChange(val) {
      this.selectionData = val
    },
    // checkSelectSet(row, index) {
    //   //row 就是每行的数据
    //   let data = this.selectionChangeData //这是多选按钮保存的数据
    //   //return : false  就是禁用
    //   //return : true  不禁用
    //   return data.findIndex((item) => item.labelId == row.labelId) === -1
    // },
    submitForm() {
      // if (this.selectionData.length === 0) {
      //   this.$message.warning('请选择要移位的' + (this.type === 'box' ? '档案盒' : '档案'))
      //   return
      // }
      this.archivesMove()
      // this.$emit('ok', this.tableData)
    },
    archivesMove() {
      if (this.queryParam.positionId === '' || this.queryParam.positionId === null) {
        this.$message.warning('请选择库位')
      } else {
        if (this.tableData.length === 0) {
          this.$message.warning('移位列表不能为空')
        } else {
          this.formData.labelList = this.tableData.map((item) => {
            return {
              labelId: item.labelId,
              boxNo: item.boxNo,
              positionId: item.positionId,
              positionName: item.positionId_dictText,
            }
          })
          this.formData.positionId = this.queryParam.positionId
          this.formData.dataSources = 0
          if (this.type === 'box') {
            boxMove(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('移位成功')
                  this.closeDialog()
                  this.$emit('ok')
                  this.tableData = []
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          } else if (this.type === 'archive') {
            archivesMove(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('移位成功')
                  this.closeDialog()
                  this.$emit('ok')
                  this.tableData = []
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          }
        }
      }
    },
    closeDialog() {
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped lang="less">
.el-table {
  display: flex;
  flex-direction: column;
}

.el-table__header-wrapper {
  overflow: unset;
}
</style>
